<?php

/**
 * This file is part of the Codizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Codizer Core Team <team@codizer.org>
 * @package     Codizer
 */

/**
 * Magic __autoload method
 * See : http://fr.php.net/autoload
 * 
 * @param string $class_name
 */
function __autoload($class_name)
{
	// we explode the class name to get the path
	$args = explode('_', $class_name);

	$firstChar = $args[0];
	$ext = '.class.php';

	switch ($firstChar) 
	{
		// if first character is "f", class is in the FRAMEWORK_HOME folder
		case 'f':
			$directory = FRAMEWORK_HOME;
		break;
		
		case 'm':
			$directory = MODULES_DIR;
		break;
		
		case 'w';
			$directory = WEBAPP_MODULES_DIR;
		break;

        case 'PHPUnit':
            $directory = FRAMEWORK_HOME.DS.'test'.DS.'PHPUnit';
            $ext = '.php';
        break;
		
        default:
            trigger_error('Loaded class is not correctly formatted : '.$class_name, E_USER_ERROR);
        break;		
	}

	// we unset the first args field to prepare implode
	unset($args[0]);

	// set the class file path
	$file = $directory.DS.implode(DS, $args).$ext;

	// check if file exist in webapp
	if(file_exists(realpath($file)))
	{
		//if true load the file
		require_once($file);
	}
	else
	{
		if($firstChar != 'w')
		{
			if(f_core_Controller::getInstance()->getExceptionReturnStatus())
			{
				trigger_error('File '.$file.' not found !', E_USER_ERROR);
			}
			else
			{
				$logger = f_logs_Logger::getInstance();
				$logger->exceptionLog('File '.$file.' not found !');
				return null;
			}
		}
	}
}